package com.oscar.ehome.view.konwledge;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.base.fragment.BaseFragment;
import com.oscar.ehome.bean.pagebean.ClassRoomBean;
import com.oscar.ehome.bean.pagebean.ClassRoomPrombleBean;
import com.oscar.ehome.presenter.ClassRoomPresenter;
import com.oscar.ehome.presenter.KnowelgePresenter;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshBase;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshListView;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.view.konwledge.adapter.KnowStudyListAdapter;
import com.oscar.ehome.view.konwledge.view.KnowSudyView;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.ClassRoomVideoActivity;
import com.oscar.ehome.view.pageview.ClassroomActivity;
import com.oscar.ehome.view.pageview.adapter.ClassoomAdapter;
import com.oscar.ehome.view.pageview.view.ClassRoomView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/9/20
 * 律师团队视频页
 */

public class KnowSudyFragment extends BaseFragment<ClassRoomView, ClassRoomPresenter> implements AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener,
        ProgressFrameLayout.AgainRequestListener, ClassRoomView {

    Unbinder unbinder;
    public View view;

    /**
     *
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     *
     */
    @BindView(R.id.knowstudy_listview)
    PullToRefreshListView knowstudyListview;
    /**
     *
     */
    @BindView(R.id.knowstudy_fragment)
    ProgressFrameLayout knowstudyFragment;
    /**
     *
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;


    String token = "";
    private int page = 1;//页数
    private int page_size = 20;//每页的条数
    String s = "1";

    KnowStudyListAdapter adapter;
    ClassRoomPresenter presenter;
    private List<ClassRoomBean.ClassRoomListBean> dataList = new ArrayList<>();
    private int size;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_konwstudy, null);
        unbinder = ButterKnife.bind(this, view);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getColor()));

        OnClicks();
        initView();
        initData();

        return view;
    }

    @Override
    protected ClassRoomPresenter createPresenter() {
        presenter = new ClassRoomPresenter(this);
        return presenter;
    }


    /**
     * 初始化
     */
    public void initView() {
        titleTopbar.setText(getString(R.string.classroom));
        token = SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getToken();

        adapter = new KnowStudyListAdapter(getActivity(), dataList);
        knowstudyListview.setAdapter(adapter);
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(getActivity())) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    /**
     * 加载数据
     */
    public void initData() {
        if (isNetWork()) {
            knowstudyFragment.stopRefresh();
            knowstudyListview.setVisibility(View.VISIBLE);
            presenter.getKnowList(token, s, "20");
            showLoading();
        } else {
            knowstudyFragment.setVisibility(View.VISIBLE);
            knowstudyFragment.setLoadFail();
            knowstudyListview.setVisibility(View.GONE);
        }
    }

    public void OnClicks() {
        knowstudyListview.setPullLoadEnabled(true);
        knowstudyListview.setOnRefreshListener(this);
        knowstudyListview.setOnItemClickListener(this);
        knowstudyFragment.setAgainRequestListener(this);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (dataList.size() > 0) {
            ClassRoomBean.ClassRoomListBean bean = dataList.get(position);
            String addapp = new Gson().toJson(bean);
            Intent intent = new Intent(getActivity(), ClassRoomVideoActivity.class);
            intent.putExtra("messagebean", addapp);
            intent.putExtra("lawyer", 0);
            startActivity(intent);
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        page = 1;
        s = String.valueOf(page);
        initData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        page++;
        s = String.valueOf(page);
        initData();
    }

    @Override
    public void againRequest() {
        if (isNetWork()) {
            initData();
        } else {
            ToastUtil.show(getActivity(), getString(R.string.pull_to_refresh_network_error));
        }
    }

    @Override
    public void knowledgeList(ClassRoomBean result) {
        dismissLoading();
        knowstudyListview.onPullDownRefreshComplete();
        knowstudyListview.onPullUpRefreshComplete();
        if (result != null) {
            if (page == 1) {
                dataList.clear(); // 将原有的内容清空
            }
            dataList = result.getList();
            if (dataList.size() > 0) {
//                knowstudyListview.setHasMoreData(true);
                knowstudyListview.setVisibility(View.VISIBLE);
                adapter.setList(dataList);
            } else {
                knowstudyListview.setVisibility(View.GONE);
                knowstudyFragment.setNoResult();
            }
        }
    }

    @Override
    public void problemList(ClassRoomPrombleBean result) {

    }

    @Override
    public void sendpromble(String result) {

    }

    @Override
    public void erroy() {
        dismissLoading();
        knowstudyListview.onPullDownRefreshComplete();
        knowstudyListview.onPullUpRefreshComplete();
        ToastUtil.show(getActivity(), getString(R.string.erry));
        knowstudyFragment.setLoadFail();
        knowstudyListview.setVisibility(View.GONE);
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        knowstudyListview.onPullDownRefreshComplete();
        knowstudyListview.onPullUpRefreshComplete();
        ToastUtil.show(getActivity(), result);
        if (result.contains("过期")) {
            getActivity().getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(getActivity()).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(getActivity()).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            getActivity().finish();
        } else {
            knowstudyFragment.setLoadFail();
            knowstudyListview.setVisibility(View.GONE);
        }
    }
}
